<?php
require 'TVB/pms/pdes/common/bootstrap.php';

empty($_REQUEST['type']) && exit('');
empty($_REQUEST['q'])    && exit('');

$query = 'select id as eps_id, onair_episode_no as eps_no, publish_datetime, expire_datetime from episode where status<>:st and ';
$param = array(':st'=>AC::STATUS_DELETED);

switch($_REQUEST['type']){
case 'eps_id':
	$query .= 'id=:id';
	$param[':id'] = (int) $_REQUEST['q'];
break;
case 'eps_no':
	empty($_REQUEST['pid']) && exit('');
	$query .= 'programme_id=:pid and onair_episode_no=:eps_no';
	$param[':pid'] = (int) $_REQUEST['pid'];
	$param[':eps_no'] = (int) $_REQUEST['q'];
break;
default:
	exit('');
}

$result	= Model::selectBySql($query,$param);
empty($result) && exit('');

$result = $result[0];

if( $result['publish_datetime'] == '0000-00-00 00:00:00'){
	$result['publish_datetime'] = array('','');
}else{
	$result['publish_datetime'] = explode(' ',$result['publish_datetime']);
	$result['publish_datetime'][1] = empty($result['publish_datetime'][1]) ? '00:00' : substr($result['publish_datetime'][1],0,5);
}

if( $result['expire_datetime'] == '0000-00-00 00:00:00'){
	$result['expire_datetime'] = array('','');
}else{
	$result['expire_datetime'] = explode(' ',$result['expire_datetime']);
	$result['expire_datetime'][1] = empty($result['expire_datetime'][1]) ? '00:00' : substr($result['expire_datetime'][1],0,5);
}

echo json_encode($result);

?>
